package com.py.credit.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.CalendarTask;
import com.py.credit.entity.SysUser;
import com.py.credit.service.CalendarTaskService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.helper.HttpServletHelper;

/**
 * 日历任务controller
 */
@RestController
@RequestMapping(value = "/calendarTask")
public class CalendarTaskController {

	@Autowired
	private CalendarTaskService calendarTaskService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	/**
	 * 添加日期任务
	 * 
	 * @param userId
	 * @param title
	 * @param description
	 * @param datetime
	 */
	@RequestMapping(value = "/add.json")
	public RestServiceResult<String> add(
			@RequestParam(value = "title", defaultValue = "", required = true) String title,
			@RequestParam(value = "description", defaultValue = "", required = true) String description,
			@RequestParam(value = "datetime", defaultValue = "", required = true) String datetime,
			HttpServletRequest request) {

		RestServiceResult<String> result = new RestServiceResult<String>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("CalendarTaskController#add", "未获取到当前用户信息！");
				return result;
			}

			calendarTaskService.add(userInfo.getId(), title.trim(), description.trim(), datetime);

			return result;

		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}

	}

	/**
	 * 修改日期任务
	 * 
	 * @param id
	 * @param title
	 * @param description
	 * @param datetime
	 */
	@RequestMapping(value = "/modify.json")
	public RestServiceResult<String> modify(@RequestParam(value = "id", defaultValue = "", required = true) int id,
			@RequestParam(value = "title", defaultValue = "", required = true) String title,
			@RequestParam(value = "description", defaultValue = "", required = true) String description,
			@RequestParam(value = "datetime", defaultValue = "", required = true) String datetime) {
		calendarTaskService.modify(id, title.trim(), description.trim(), datetime);

		return new RestServiceResult<String>();
	}

	/**
	 * 删除日期任务
	 * 
	 * @param id
	 */
	@RequestMapping(value = "/del.json")
	public RestServiceResult<String> del(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		calendarTaskService.del(id);

		return new RestServiceResult<String>();
	}

	/**
	 * 查询日期任务
	 * 
	 * @param userId
	 */
	@RequestMapping(value = "/getCalendarTask.json")

	public RestServiceResult<List<CalendarTask>> getCalendarTask(HttpServletRequest request) {

		RestServiceResult<List<CalendarTask>> result = new RestServiceResult<List<CalendarTask>>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("CalendarTaskController#getCalendarTask", "未获取到当前用户信息！");
				return result;
			}

			result.setData(calendarTaskService.getCalendarTask(userInfo.getId()));

			return result;

		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}

	}
}
